package com.android.lovingpet.community.post;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.common.PostModel;
import com.android.lovingpet.util.Utility;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PostPresenter extends BasePresenter<PostContract.View> implements PostContract.Presenter {

    @Override
    public void post(String context, List<String> album, int type) {
        if (TextUtils.isEmpty(context)) {
            getView().showToast("请输入内容、、");
            return;
        }
        getView().showProgress();
        List<File> files = new ArrayList<>();
        for (String string : album) {
            if (string != null) {
                files.add(new File(string));
            }
        }
        int postType = 0;
        if (type == 5) {
            postType = 3;
        }
        PostModel.getInstance().post(context, type, 0, postType, null, null, null, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                upload(bundle.getInt("id"), files);
            }
        });
    }

    private void upload(int id, List<File> album) {
        if (album == null || album.isEmpty()) {
            getView().next();
            return;
        }
        FileModel.getInstance().uploadAlbum(id, Utility.getUserType(), 0, album, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().hideProgress();
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getView().next();
            }
        });
    }
}
